package com.aipartner.service.impl;

import com.aipartner.dto.AssistantFunctionResponse;
import com.aipartner.service.AssistantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Assistant Service Implementation
 * 
 * @author AI Partner Team
 * @since 2024-01-20
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AssistantServiceImpl implements AssistantService {
    
    @Override
    public List<Map<String, Object>> getAllCategories() {
        List<Map<String, Object>> categories = new ArrayList<>();
        
        // Writing category
        Map<String, Object> writing = Map.of(
            "id", "writing",
            "name", "Writing Assistant",
            "icon", "edit",
            "description", "Help with various writing tasks",
            "functionCount", 6
        );
        categories.add(writing);
        
        // Business category
        Map<String, Object> business = Map.of(
            "id", "business",
            "name", "Business Assistant",
            "icon", "briefcase",
            "description", "Business and professional tools",
            "functionCount", 5
        );
        categories.add(business);
        
        // Study category
        Map<String, Object> study = Map.of(
            "id", "study",
            "name", "Study Assistant",
            "icon", "book",
            "description", "Learning and education support",
            "functionCount", 4
        );
        categories.add(study);
        
        // Life category
        Map<String, Object> life = Map.of(
            "id", "life",
            "name", "Life Assistant",
            "icon", "home",
            "description", "Daily life helper",
            "functionCount", 6
        );
        categories.add(life);
        
        // Creative category
        Map<String, Object> creative = Map.of(
            "id", "creative",
            "name", "Creative Assistant",
            "icon", "palette",
            "description", "Creative and artistic support",
            "functionCount", 5
        );
        categories.add(creative);
        
        // Tech category
        Map<String, Object> tech = Map.of(
            "id", "tech",
            "name", "Tech Assistant",
            "icon", "code",
            "description", "Programming and technical help",
            "functionCount", 6
        );
        categories.add(tech);
        
        // Language category
        Map<String, Object> language = Map.of(
            "id", "language",
            "name", "Language Assistant",
            "icon", "translate",
            "description", "Language learning and translation",
            "functionCount", 4
        );
        categories.add(language);
        
        // Health category
        Map<String, Object> health = Map.of(
            "id", "health",
            "name", "Health Assistant",
            "icon", "heart",
            "description", "Health and wellness guidance",
            "functionCount", 3
        );
        categories.add(health);
        
        // Entertainment category
        Map<String, Object> entertainment = Map.of(
            "id", "entertainment",
            "name", "Entertainment Assistant",
            "icon", "game",
            "description", "Fun and entertainment tools",
            "functionCount", 3
        );
        categories.add(entertainment);
        
        return categories;
    }
    
    @Override
    public List<AssistantFunctionResponse> getFunctionsByCategory(String categoryId) {
        List<AssistantFunctionResponse> functions = new ArrayList<>();
        
        switch (categoryId) {
            case "writing":
                functions.add(new AssistantFunctionResponse("article_writer", "Article Writer", 
                    "Generate professional articles", "edit-document", true, false));
                functions.add(new AssistantFunctionResponse("email_writer", "Email Writer", 
                    "Compose professional emails", "email", false, false));
                functions.add(new AssistantFunctionResponse("social_media", "Social Media Posts", 
                    "Create engaging social media content", "share", false, false));
                functions.add(new AssistantFunctionResponse("blog_ideas", "Blog Ideas", 
                    "Generate creative blog topics", "lightbulb", false, false));
                functions.add(new AssistantFunctionResponse("proofreader", "Proofreader", 
                    "Check grammar and style", "check-circle", false, false));
                functions.add(new AssistantFunctionResponse("summarizer", "Text Summarizer", 
                    "Summarize long texts", "compress", false, false));
                break;
                
            case "business":
                functions.add(new AssistantFunctionResponse("business_plan", "Business Plan", 
                    "Create business plans", "chart-line", true, false));
                functions.add(new AssistantFunctionResponse("market_analysis", "Market Analysis", 
                    "Analyze market trends", "chart-bar", false, true));
                functions.add(new AssistantFunctionResponse("meeting_notes", "Meeting Notes", 
                    "Organize meeting minutes", "clipboard-list", false, false));
                functions.add(new AssistantFunctionResponse("proposal_writer", "Proposal Writer", 
                    "Write business proposals", "handshake", false, true));
                functions.add(new AssistantFunctionResponse("swot_analysis", "SWOT Analysis", 
                    "Strategic analysis tool", "balance-scale", false, false));
                break;
                
            case "study":
                functions.add(new AssistantFunctionResponse("study_planner", "Study Planner", 
                    "Create study schedules", "calendar-alt", true, false));
                functions.add(new AssistantFunctionResponse("quiz_generator", "Quiz Generator", 
                    "Generate practice quizzes", "question-circle", false, false));
                functions.add(new AssistantFunctionResponse("note_organizer", "Note Organizer", 
                    "Organize study notes", "sticky-note", false, false));
                functions.add(new AssistantFunctionResponse("research_helper", "Research Helper", 
                    "Assist with research", "search", false, true));
                break;
                
            case "life":
                functions.add(new AssistantFunctionResponse("meal_planner", "Meal Planner", 
                    "Plan weekly meals", "utensils", true, false));
                functions.add(new AssistantFunctionResponse("budget_tracker", "Budget Tracker", 
                    "Track personal finances", "wallet", false, false));
                functions.add(new AssistantFunctionResponse("travel_planner", "Travel Planner", 
                    "Plan your trips", "map-marked-alt", false, false));
                functions.add(new AssistantFunctionResponse("gift_ideas", "Gift Ideas", 
                    "Find perfect gifts", "gift", false, false));
                functions.add(new AssistantFunctionResponse("home_organizer", "Home Organizer", 
                    "Organize your space", "home", false, false));
                functions.add(new AssistantFunctionResponse("habit_tracker", "Habit Tracker", 
                    "Build good habits", "check-double", false, false));
                break;
                
            case "creative":
                functions.add(new AssistantFunctionResponse("story_writer", "Story Writer", 
                    "Create creative stories", "book-open", true, false));
                functions.add(new AssistantFunctionResponse("poetry_generator", "Poetry Generator", 
                    "Generate beautiful poems", "feather-alt", false, false));
                functions.add(new AssistantFunctionResponse("art_ideas", "Art Ideas", 
                    "Creative art inspiration", "palette", false, false));
                functions.add(new AssistantFunctionResponse("music_composer", "Music Composer", 
                    "Compose music pieces", "music", false, true));
                functions.add(new AssistantFunctionResponse("design_helper", "Design Helper", 
                    "Design assistance", "pencil-ruler", false, false));
                break;
                
            case "tech":
                functions.add(new AssistantFunctionResponse("code_generator", "Code Generator", 
                    "Generate code snippets", "code", true, false));
                functions.add(new AssistantFunctionResponse("bug_fixer", "Bug Fixer", 
                    "Debug code issues", "bug", false, false));
                functions.add(new AssistantFunctionResponse("api_designer", "API Designer", 
                    "Design REST APIs", "cogs", false, true));
                functions.add(new AssistantFunctionResponse("database_designer", "Database Designer", 
                    "Design database schemas", "database", false, true));
                functions.add(new AssistantFunctionResponse("tech_docs", "Tech Documentation", 
                    "Write technical docs", "file-code", false, false));
                functions.add(new AssistantFunctionResponse("system_architect", "System Architect", 
                    "Design system architecture", "sitemap", false, true));
                break;
                
            case "language":
                functions.add(new AssistantFunctionResponse("translator", "Translator", 
                    "Translate languages", "language", true, false));
                functions.add(new AssistantFunctionResponse("grammar_teacher", "Grammar Teacher", 
                    "Learn grammar rules", "graduation-cap", false, false));
                functions.add(new AssistantFunctionResponse("vocabulary_builder", "Vocabulary Builder", 
                    "Expand vocabulary", "spell-check", false, false));
                functions.add(new AssistantFunctionResponse("conversation_practice", "Conversation Practice", 
                    "Practice conversations", "comments", false, false));
                break;
                
            case "health":
                functions.add(new AssistantFunctionResponse("fitness_planner", "Fitness Planner", 
                    "Create workout plans", "dumbbell", true, false));
                functions.add(new AssistantFunctionResponse("nutrition_advisor", "Nutrition Advisor", 
                    "Nutritional guidance", "apple-alt", false, false));
                functions.add(new AssistantFunctionResponse("wellness_tracker", "Wellness Tracker", 
                    "Track wellness metrics", "heart", false, false));
                break;
                
            case "entertainment":
                functions.add(new AssistantFunctionResponse("joke_generator", "Joke Generator", 
                    "Generate funny jokes", "laugh", false, false));
                functions.add(new AssistantFunctionResponse("trivia_quiz", "Trivia Quiz", 
                    "Fun trivia questions", "puzzle-piece", false, false));
                functions.add(new AssistantFunctionResponse("word_games", "Word Games", 
                    "Play word games", "gamepad", false, false));
                break;
        }
        
        return functions;
    }
    
    @Override
    public List<AssistantFunctionResponse> getAllFunctions() {
        return getAllCategories().stream()
                .map(category -> (String) category.get("id"))
                .flatMap(categoryId -> getFunctionsByCategory(categoryId).stream())
                .collect(Collectors.toList());
    }
    
    @Override
    public AssistantFunctionResponse getFunctionDetails(String functionId) {
        return getAllFunctions().stream()
                .filter(function -> function.getId().equals(functionId))
                .findFirst()
                .orElse(null);
    }
    
    @Override
    public List<AssistantFunctionResponse> searchFunctions(String keyword) {
        return getAllFunctions().stream()
                .filter(function -> 
                    function.getName().toLowerCase().contains(keyword.toLowerCase()) ||
                    function.getDescription().toLowerCase().contains(keyword.toLowerCase()))
                .collect(Collectors.toList());
    }
    
    @Override
    public List<AssistantFunctionResponse> getHotRecommendations() {
        return getAllFunctions().stream()
                .filter(function -> function.getPopular() != null && function.getPopular())
                .limit(6)
                .collect(Collectors.toList());
    }
}